package com.own.core.html;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.wicket.markup.html.form.FormComponent;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;

import com.own.core.html.form.OwnForm;
import com.own.core.html.form.checkBox.CheckBox;
import com.own.core.html.form.dateField.DateField;
import com.own.core.html.form.dropDown.DropDown;
import com.own.core.html.form.password.Password;
import com.own.core.html.form.textArea.TextArea;
import com.own.core.html.form.textField.TextField;
import com.own.core.html.panel.OwnDynPanel;
import com.own.core.html.panel.checkBox.OwnCheckBoxPan;
import com.own.core.html.panel.dateField.OwnDateFieldPan;
import com.own.core.html.panel.dropdown.OwnDropDownPan;
import com.own.core.html.panel.empty.OwnEmptyPan;
import com.own.core.html.panel.password.OwnPasswordFieldPan;
import com.own.core.html.panel.textArea.OwnTextAreaPan;
import com.own.core.html.panel.textField.OwnTextFieldPan;
import com.own.util.model.OwnCompoundPropertyModel;
 
public class ListViewObject extends ListView {

	Map<String, FormComponent > componentMap=new HashMap<String, FormComponent >();
	public ListViewObject() {
		super("viewList");

	}

	OwnCompoundPropertyModel model;
	OwnForm form;

	public ListViewObject(  OwnCompoundPropertyModel model, List list,
			OwnForm form) {

		super("viewList", list);
		this.model = model;
		this.form = form;
	}

	@Override
	protected void populateItem(ListItem item) {

		Field f = (Field) item.getModelObject();
		Annotation[] annotations = f.getAnnotations();
		OwnDynPanel panel = null;
		// System.out.println(f.getName());
		for (Annotation ann : annotations) {

			if (ann instanceof TextField) {
				panel = drawTextField(item, f, (TextField) ann);

			} else if (ann instanceof DateField) {
				panel = drawDateField(item, f, (DateField) ann);

			} else if (ann instanceof TextArea) {
				panel = drawTextArea(item, f, (TextArea) ann);

			} else if (ann instanceof DropDown) {
				panel = drawDropDown(item, f, (DropDown) ann);

			} else if (ann instanceof CheckBox) {
				panel = drawCheckBox(item, f, (CheckBox) ann);

			} else if (ann instanceof Password) {
				panel = drawPassword(item, f, (Password) ann);

			}// end if else

			// add panel
			if (panel != null) {
				item.add(panel);
				componentMap.put(f.getName(), panel.getFormComponent());
				return;

			}
		}
		item.add(new OwnEmptyPan());
	
	}

	OwnDynPanel drawTextField(ListItem item, Field field, TextField textfield) {
		OwnDynPanel panel = new OwnTextFieldPan(model, field, textfield);
		panel.setRequired(textfield.required());

		return panel;

	}

	OwnDynPanel drawDateField(ListItem item, Field field, DateField dateField) {
		OwnDynPanel panel = new OwnDateFieldPan(model, field, dateField);
		panel.setRequired(dateField.required());

		return panel;

	}

	OwnDynPanel drawPassword(ListItem item, Field field, Password password) {
		OwnDynPanel panel = new OwnPasswordFieldPan(model, field, password,
				form);
		panel.setRequired(password.required());

		return panel;

	}

	OwnDynPanel drawTextArea(ListItem item, Field field, TextArea textArea) {

		OwnDynPanel panel = new OwnTextAreaPan(model, field, textArea);
		panel.setRequired(textArea.required());
		return panel;
	}

	OwnDynPanel drawDropDown(ListItem item, Field field, DropDown dropDown) {
		OwnDropDownPan panel = new OwnDropDownPan(model, field, dropDown);
		panel.setRequired(dropDown.required());
		panel.setDropDown(dropDown);
		return panel;
	}

	OwnDynPanel drawCheckBox(ListItem item, Field field, CheckBox checkBox) {
		OwnCheckBoxPan panel = new OwnCheckBoxPan(model, field, checkBox);

		return panel;
	}

	public Map<String, FormComponent> getComponentMap() {
		return componentMap;
	}

	public void setComponentMap(Map<String, FormComponent> componentMap) {
		this.componentMap = componentMap;
	}

}
